package com.kuang.service;

import com.kuang.bean.Product;
import com.kuang.bean.ProductExample;
import com.kuang.dao.ProductMapper;
import com.kuang.utils.GetTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    GetTime getTime = new GetTime();
    ProductExample productExample = new ProductExample();
    ProductExample.Criteria criteria = productExample.createCriteria();

    public ProductServiceImpl(ProductMapper productMapper) {
        this.productMapper = productMapper;
    }

    public int add(Product product) {
        return productMapper.insert(product);
    }


    public int deleteProductById(int id) {
        return productMapper.deleteByPrimaryKey(id);
    }

    public int updateProduct(Product product) {
        return productMapper.updateByPrimaryKey(product);
    }

    public Product queryProductById(int id) {
        return productMapper.selectByPrimaryKey(id);
    }

    public List<Product> queryByType(int type) {
        criteria.andTypeEqualTo(type);
        return productMapper.selectByExample(productExample);
    }

    public List<Product> queryAllByTime() {
        criteria.andCreateTimeBetween(getTime.getBefore(),getTime.getNow());
        return productMapper.selectByExample(productExample);
    }

    public List<Product> queryByPrice(double a, double b) {
        criteria.andPriceBetween(a,b);
        return productMapper.selectByExample(productExample);
    }

    public List<Product> queryByName(String m) {
        criteria.andProductNameEqualTo(m);
        return productMapper.selectByExample(productExample);
    }

    public List<Product> queryAllProduct() {
        ProductExample productExample = new ProductExample();
        return productMapper.selectByExample(productExample);
    }
}
